"""
    Controller for topic editing.
"""

from turbogears import widgets, identity, validate, expose, redirect
from turbogears.widgets import TextField
from mnemoply import model
from mnemoply.controllers.lookup import LookupController, TableCol, TableLookupDataSource
from mnemoply.controllers.crud import CRUDController, ItemActionsTab, ItemTab

class PlanItemTab(ItemActionsTab):
    title = _("Plans")
    name = "plans_tab"
    ma_class_name = 'Plan'

    def getRecords(self, obj):
        planTopics = list(model.PlanTopic.selectBy(topic=obj))
        from sets import Set
        plans = Set()
        for ptopic in planTopics:
            plans |= Set([ptopic.plan])
        plans = list(plans)
        plans.sort(key=lambda x: x.title)
        return plans

    def getRecordTitle(self, obj):
        return obj.title

class FinalWorkTopicItemTab(ItemTab):
    title = _("Final works")
    name = "final_works_tab"
    template = "../final_works_tab"

    add_form = widgets.TableForm(
        fields=[
            TextField(name="name", label=_("Name"), value="Name"),
            TextField(name="comment", label=_("Comment"), value="Comment"),
            ],
        submit_text=_("Add"),
        action='add_final_work',
        )

    @identity.require(identity.has_any_permission('dba', 'FinalWorkTopic/edit'))
    @validate(form=add_form)
    @expose()
    def add_final_work(self, obj, name, comment):
        try:
            if comment is None:
                comment = u''
            model.FinalWorkTopic(topic=obj, name=name, comment=comment)
        except:
            pass
        raise redirect("./")

    @identity.require(identity.has_any_permission('dba', 'FinalWorkTopic/edit'))
    @expose()
    def delete_finan_work(self, obj, id):
        fw = model.FinalWorkTopic.get(int(id))
        fw.destroySelf()
        raise redirect("./")

class TopicController(LookupController, CRUDController):
    model_object = model.Topic

    lookup_columns = [
        TableCol(name='id', caption=_("#"), size=4),
        TableCol(name='faculty', caption=_("Faculty")),
        TableCol(name='name', caption=_("Name")),
        TableCol(name='teacher', caption=_("Teacher")),
        TableCol(name='final_test', caption=_("Final test"), hidden=True),
        TableCol(name='comment', caption=_("Comment"), hidden=True),
        TableCol(name='is_final_work', caption=_("Is final work"), hidden=True),
        TableCol(name='should_be_a_mark', caption=_("Should be a mark"), hidden=True),
        TableCol(name='trend', caption=_("Trend"), hidden=True),
        ]

    view_fields = [
        'name',
        'trendID',
        'responsibleTeacherID',
        'finalTestID',
        'isFinalWork',
        'shouldBeAMark',
        'comment'
        ]

    item_tabs = [PlanItemTab(), FinalWorkTopicItemTab()]

    def __init__(self):
        LookupController.__init__(self,
            data_source=TableLookupDataSource('topic_lookup'),
            columns=self.lookup_columns,
            mass_actions_class=self.model_object,
            title=self.model_object.model_title,
        )
        CRUDController.__init__(self, model_object=self.model_object)

exposed = TopicController()
